package com.hanbit.eatery;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hanbit.common.ThePager;
import com.hanbit.eatery.service.Eatery;
import com.hanbit.eatery.service.EateryService;


@Controller
public class EateryController {
	
	@Autowired
	private EateryService service;
	
	@RequestMapping(value="/ewrite.do",method=RequestMethod.GET)
	public String writeGet() {
		return "front/category/ewrite";
	}
	
	@RequestMapping(value="/ewrite.do",method=RequestMethod.POST)
	public String writePost(Eatery eatery,HttpServletRequest request) {
		int maxNum=service.maxNum();
		eatery.seteNum(maxNum+1);
		service.insertEatery(eatery);
		return "redirect:eatery.do";
	}
	
	@RequestMapping("/eatery.do")
	public String list(HttpServletRequest request,Model model) {
        String category=request.getParameter("category");
        int totalEatery=service.getCategoryCount(category);

        List<Eatery> eateryCategory=service.getEateryCategory(category);

        model.addAttribute("eateryCategory", eateryCategory);
        model.addAttribute("totalEatery", totalEatery);
		
		return "front/category/eatery";
	}
	
	@RequestMapping(value="/edelete",method=RequestMethod.POST)
	public String deletePost(int eNum) {
		service.deleteEatery(eNum);
		return "redirect:eatery.do";
	}
	
	@RequestMapping(value="/eupdate.do",method=RequestMethod.GET)
	public String updateGet() {
		return "front/category/eupdate";
	}
	
	@RequestMapping(value="/eupdate.do",method=RequestMethod.POST)
	public String updatePost(Eatery eatery, HttpServletRequest request) {
		service.updateEatery(eatery);
		return "redirect:eatery.do";
	}
	
	@RequestMapping(value="/edetail.do",method=RequestMethod.GET)
	public String detailGet(int eNum) {
		service.getEatery(eNum);
		return "front/category/edetail";
	}
	
}